/**********************************************************
FileName: poll.c
Author: Version : Date: Casiawu(wujh@emsyschina.com)
Description: // 网关 模拟 客户端   2018.6.21
1. -------
History: // 历史修改记
<author> <time> <version > <desc>  asasasa
casiawu 2008/2/12 1.0 build this module for linux (2.6.21)
***********************************************************/
#include <stdio.h>
#include <sys/types.h>  
#include <sys/stat.h>    
#include <errno.h>     
#include <string.h>
#include <stdlib.h>  
#include <unistd.h>  
#include <sys/socket.h>
#include <netinet/tcp.h> 
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>

#include "gateway.h"

void *fun(void *argc);

char sendbuf[128];

int main(int argc, char *argv[])
{
	int ret;
	int confd,maxfd;
	char buf[128] = {0};
	fd_set readfs;
	struct timeval timeout={0,1}; 
	int cnt = 0;
	
	confd = ClientInit("192.168.0.100", 6666);
	if(confd < 0){
		printf("ClientInit error \n");
		return -1;
	}
	printf("client_init success \n");
	SetNonblockMode(confd);
	maxfd = confd + 1;
	
	while(1){
		FD_ZERO(&readfs);
		FD_SET(confd, &readfs);

		cnt++;
		delay(0, 50);
		if(cnt%20 == 0){
			//1s  发送心跳

		}
		
		ret = select(maxfd, &readfs, NULL, NULL, NULL);
		if(ret < 0){
			printf("select error \n");
			close(confd);
			return -1;
		}else{
			if(FD_ISSET(confd,&readfs)){
				bzero(buf, sizeof(buf));
				ret = recv(confd, buf, sizeof(buf), 0);
				if(ret<=0){
					perror("recv");
					printf("errno : %d \n",errno);
					close(confd);
					return -1;
				}
				printf("buf: %s,ret = %d \n", buf, ret);
			}
		}
	}
}

